package com.example.coffeeseller_2.model.model;

import android.content.Context;
import android.text.TextUtils;

import com.example.coffeeseller_2.model.beans.ReportBunker;
import com.example.coffeeseller_2.model.contentprovider.MaterialSql;
import com.example.coffeeseller_2.utils.MyLog;

/**
 * Created by WH on 2018/6/21.
 * 更新用水量
 */

public class UpdateWaterMaterial {
    private static String TAG = "UpdateWaterMaterial";

    public static ReportBunker update(Context context, int waterUseTotal) {

        MaterialSql materialSql = new MaterialSql(context);
        String waterMaterialId = materialSql.getMaterialIDByContainerID("7");
        if (waterUseTotal != 0 && waterMaterialId != null && !TextUtils.isEmpty(waterMaterialId)) {

            String sqlRestWaterMaterial = materialSql.getStorkByMaterialID(waterMaterialId + "");

            float sqlRestWaterMaterialInt = Float.parseFloat(sqlRestWaterMaterial);


            int waterStock = (int) (sqlRestWaterMaterialInt - waterUseTotal / 10);

            boolean isUpdateSuccess = materialSql.updateMaterialStockByMaterialId(waterMaterialId + "", waterStock + "");

            MyLog.d(TAG, "用水量更新：materialID is" + waterMaterialId + " , rest= "
                    + sqlRestWaterMaterialInt + ", used= " + waterUseTotal / 10
                    + " , stock= " + waterStock + ", isUpdateSuccess= " + isUpdateSuccess);


            int bunkerId = Integer.parseInt(materialSql.getBunkerIDByMaterialD(waterMaterialId + ""));
            ReportBunker reportBunker = new ReportBunker();
            reportBunker.setBunkerID(bunkerId);
            reportBunker.setUnit(Math.round(waterUseTotal) + "");
            reportBunker.setMaterialStock(Math.round(waterStock) + "");
            return reportBunker;
        }
        return null;
    }
}
